//
//  XPPXcodePreferences.m
//  XPPXcodePlugin
//
//  Created by yoyokko on 10-4-21.
//  Copyright 2010 yoyokko@gmail.com. All rights reserved.
//

#import "XPPXcodePreferences.h"
#import "XPPXcodePlugin.h"
#import "GTMObjC2Runtime.h"
#import "PBXPreferencesModule.h"
#import "XPPAutoCompleteHelper.h"
#import "XPPXcodeRegisterMenuItem.h"

NSString * const XPPXcodePreferencesMenuItemPrefChanged = @"XPPXcodePreferencesMenuItemPrefChanged";
NSString * const XPPXCodeSuppressMenuItemIcon = @"XPPXCodeSuppressMenuItemIcon";
NSString * const XPPXCodeEnableDefaultKey = @"XPPXCodeEnableDefaultKey";
NSString * const XPPXCodeEnableUseDefinedKey = @"XPPXCodeEnableUseDefinedKey";
NSString * const XPPXcodeXppDefaultKey = @"XPPXcodeXppDefaultKey";
NSString * const XPPXcodeSelectAllToReindent = @"XPPXcodeSelectAllToReindent";


@implementation XPPXcodePreferences

// Set our minimum size for the pane
- (NSSize)minModuleSize {
	return NSMakeSize(617, 362);
}

- (IBAction) reloadUserDefinedFile:(id) sender
{
	[[XPPAutoCompleteHelper sharedXPPAutoCompleteHelper] loadXcodeKeyTextFile];
}

- (IBAction) openTheFile:(id) sender
{
	NSString *plistFilePath = [@"/Library/Application Support/XPP/xcodekeytext.plist" stringByStandardizingPath];
	[[NSWorkspace sharedWorkspace] openFile:plistFilePath];
}

- (IBAction) detectKeyboardLayout:(id) sender
{
	
}

- (IBAction) checkBoxAction:(id) sender
{
	if (sender == enableUseDefinedKeys_ && [enableUseDefinedKeys_ state] == NSOnState) {
		[conflictTypeRadio_ setEnabled:YES];
	}else if (sender == enableUseDefinedKeys_ && [enableUseDefinedKeys_ state] == NSOffState) {
		[conflictTypeRadio_ setEnabled:NO];
	}

}

- (IBAction) keyboardLayoutSelectionChanged:(id) sender
{
	int tag = [keyboardLayoutType_ selectedTag];
	[self showPreviewImageWithTag:tag];
}

- (void) showPreviewImageWithTag:(int) tag
{
	NSImage *theImage;
	switch (tag) {
		case 0:
			theImage = [[NSImage alloc] initWithContentsOfFile:[[XPPXcodePlugin pluginBundle] pathForResource:@"US" ofType:@"png"]];
			break;
		case 1:
			theImage = [[NSImage alloc] initWithContentsOfFile:[[XPPXcodePlugin pluginBundle] pathForResource:@"Dvorak" ofType:@"png"]];
			break;
		case 2:
			theImage = [[NSImage alloc] initWithContentsOfFile:[[XPPXcodePlugin pluginBundle] pathForResource:@"Dvorak-Left" ofType:@"png"]];
			break;
		case 3:
			theImage = [[NSImage alloc] initWithContentsOfFile:[[XPPXcodePlugin pluginBundle] pathForResource:@"Dvorak-Qwerty" ofType:@"png"]];
			break;
		case 4:
			theImage = [[NSImage alloc] initWithContentsOfFile:[[XPPXcodePlugin pluginBundle] pathForResource:@"Dvorak-Right" ofType:@"png"]];
			break;
		default:
			break;
	}
	[previewImage setImage:theImage];
	[theImage release];
}

// Return our nice little icon
- (id)imageForPreferenceNamed:(id)parameter1 {
	NSBundle *bundle = [XPPXcodePlugin pluginBundle];
	NSString *path =  [bundle pathForImageResource:@"XPP"];
	NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
	[image setScalesWhenResized:YES];
	[image setSize:NSMakeSize(32, 32)];
	return image;
}

// This gets called everytime preferences are pulled up so that we can
// set up our state.
- (void)initializeFromDefaults {
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	
	NSDictionary *dDict = [defaults dictionaryRepresentation];
	if( ![[dDict allKeys] containsObject:@"XPPXCodeSuppressMenuItemIcon"])
	{
		[defaults setBool:YES forKey:XPPXCodeSuppressMenuItemIcon];
	}
	
	BOOL isRegistered = [XPPXcodeRegisterMenuItem isRegistered];
	// Handle menuitem icon (pref is suppress, button is show)
	// NOTE: this preference is negative, but the UI works as the positive, this
	// is done so the lack of the preference (ie-the default) will turn on the
	// icons.
	NSInteger state = NSOnState;
	if (![defaults boolForKey:XPPXCodeSuppressMenuItemIcon]) {
		state = NSOffState;
	}
	[showImageOnMenuItems_ setState:state];

	state = NSOnState;
	if (![defaults boolForKey:XPPXCodeEnableDefaultKey]) {
		state = NSOffState;
	}
	[enableDefaultKeys_ setState:state];
	
	state = NSOnState;
	if (![defaults boolForKey:XPPXCodeEnableUseDefinedKey]) {
		state = NSOffState;
	}
	[enableUseDefinedKeys_ setState:state];
	
	state = NSOnState;
	if (![defaults boolForKey:XPPXcodeSelectAllToReindent]) {
		state = NSOffState;
	}
	[selectionAllToReindent_ setState:state];
	if (!isRegistered) {
		[selectionAllToReindent_ setEnabled:NO];
		[reloadUserDefinedFile_ setEnabled:NO];
		[enableUseDefinedKeys_ setEnabled:NO];
	}else {
		[selectionAllToReindent_ setEnabled:YES];
		[enableUseDefinedKeys_ setEnabled:YES];
		[reloadUserDefinedFile_ setEnabled:YES];
	}
	
	int value = [defaults integerForKey:XPPXcodeXppDefaultKey];
	[conflictTypeRadio_ selectCellWithTag:value];
	
	[self checkBoxAction:enableUseDefinedKeys_];
}

// This gets called on Apply or OK is "hasChangesPending" returns YES.
- (void)saveChanges {
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	
	// Handle menuitem icon (pref is suppress, button is show)
	// NOTE: this preference is negative, but the UI works as the positive, this
	// is done so the lack of the preference (ie-the default) will turn on the
	// icons.
	BOOL newSetting = ([showImageOnMenuItems_ state] == NSOnState);
	[defaults setBool:newSetting forKey:XPPXCodeSuppressMenuItemIcon];
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:XPPXcodePreferencesMenuItemPrefChanged
					  object:self];
	
	BOOL setting = ([enableDefaultKeys_ state] == NSOnState);
	[defaults setBool:setting forKey:XPPXCodeEnableDefaultKey];
	
	setting = ([enableUseDefinedKeys_ state] == NSOnState);
	[defaults setBool:setting forKey:XPPXCodeEnableUseDefinedKey];
	
	setting = ([selectionAllToReindent_ state] == NSOnState);
	[defaults setBool:setting forKey:XPPXcodeSelectAllToReindent];
	
	[defaults setInteger:[conflictTypeRadio_ selectedTag] forKey:XPPXcodeXppDefaultKey];
	
	// save out our settings
	[defaults synchronize];
}


// Currently we'll always return YES as it doesn't hurt at all.
- (BOOL)hasChangesPending {
	return YES;
}

+ (BOOL)showImageOnMenuItems {
	// NOTE: this preference is negative, but the UI works as the positive, this
	// is done so the lack of the preference (ie-the default) will turn on the
	// icons.
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults boolForKey:XPPXCodeSuppressMenuItemIcon];
}

@end
